package org.meichao.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置拦截器,解决跨域问题(拦截器+配置类(WebMvcConfig))
 */
@Component
public class AccessInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //System.out.println("要想不被我拦截,就去WebMvcConfig中设置哦");
        //设置的域名
        response.setHeader("Access-Control-Allow-Origin","*");
        //跨域允许的请求方式
        response.setHeader("Access-Control-Allow-Methods","*");
        //跨域允许包含的头
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type-authentication");
        return true;
    }
}
